﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;

namespace GPS.Controllers
{
    public class GPS_HocVienHoSoController : FDBController
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public ActionResult Index(ViewModelDHV_HS SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<DHV_HS>(o => (SearchModel.pHV_HS_HV_MA == null || o.HV_HS_HV_MA.StartsWith(SearchModel.pHV_HS_HV_MA))
                                                             && (SearchModel.pHV_HS_SOHS == null || o.HV_HS_HOSO_MA == SearchModel.pHV_HS_HV_MA)
                                                             && (SearchModel.pHV_HS_DVGTVT_MA == null || o.HV_HS_DVGTVT_MA == SearchModel.pHV_HS_DVGTVT_MA)
                                                             && (SearchModel.pHV_HS_NGAY_NHANHS_TU == null || (o.HV_HS_NGAY_NHANHS >= SearchModel.pHV_HS_NGAY_NHANHS_TU))
                                                             && (SearchModel.pHV_HS_NGAY_NHANHS_DEN == null || (o.HV_HS_NGAY_NHANHS <= SearchModel.pHV_HS_NGAY_NHANHS_DEN))
                                                             );

            var model = new List<DHV_HS>();
            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<DHV_HS>(currentEntity));
            }

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            return View(SearchModel);
        }

        public ActionResult Create()
        {
            initialCategoryCreateAction();

            return View(new CrudModelDHV_HS());
        }

        [HttpPost]
        public ActionResult Create(CrudModelDHV_HS model)
        {
            if (ModelState.IsValid)
            {

                DHV_HS _model = new DHV_HS();
                GPS.COMMON.Helpers.CopyObject<DHV_HS>(model, ref _model, new List<string> { "HV_HS_ACTIVE" });
                _model.HV_HS_ACTIVE = (model.HV_HS_ACTIVE == false ? "0" : "1");
                _model.ID = EFSequence.GetNextValue("SEQ_DHV_HS_ID");
                if (!string.IsNullOrWhiteSpace(model.HV_HS_DVGTVT_MA))
                {
                    _model.HV_HS_DVGTVT_MA = model.HV_HS_DVGTVT_MA.Split(':')[0];
                }

                if (!string.IsNullOrWhiteSpace(model.HV_HS_DVGTVT_MA_NHANHS))
                {
                    _model.HV_HS_DVGTVT_MA_NHANHS = model.HV_HS_DVGTVT_MA_NHANHS.Split(':')[0];
                }

                if (!string.IsNullOrWhiteSpace(model.HV_HS_DVGTVT_MA_GPLXDACO))
                {
                    _model.HV_HS_DVGTVT_MA_GPLXDACO = model.HV_HS_DVGTVT_MA_GPLXDACO.Split(':')[0];
                }

                _model.HV_HS_NGUOINHAP = User.Identity.Name;
                _model.HV_HS_NGAYNHAP = DateTime.Now;

               // _model.HV_HS_DATRUYEN = "0";

                if (model.vHV_HS_ANH != null)
                {
                    HttpPostedFileBase file = model.vHV_HS_ANH;
                    using (var binaryReader = new BinaryReader(file.InputStream))
                    {
                        _model.HV_HS_ANH = binaryReader.ReadBytes(file.ContentLength);
                    }

                }
                

                _db.Insert<DHV_HS>(_model);

                return RedirectToAction("Index");
            }

            return View(model);
        }

        public ActionResult Edit(decimal id )
        {
           
            DHV_HS model = _db.GetOne<DHV_HS>(o => o.ID==id);

            CrudModelDHV_HS _model = new CrudModelDHV_HS();
            GPS.COMMON.Helpers.CopyObject<CrudModelDHV_HS>(model, ref _model, new List<string> { "HV_HS_ACTIVE" });

            var dvgtvt = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == model.HV_HS_DVGTVT_MA);
            _model.HV_HS_DVGTVT_MA = dvgtvt.DVGTVT_MA + ":" + dvgtvt.DVGTVT_TEN;
            if (!string.IsNullOrWhiteSpace(model.HV_HS_DVGTVT_MA_NHANHS))
            {
                var dvgtvt1 = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == model.HV_HS_DVGTVT_MA_NHANHS);
                _model.HV_HS_DVGTVT_MA_NHANHS = dvgtvt1.DVGTVT_MA + ":" + dvgtvt1.DVGTVT_TEN;
            }

            if (!string.IsNullOrWhiteSpace(model.HV_HS_DVGTVT_MA_GPLXDACO))
            {
                var dvgtvt2 = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == model.HV_HS_DVGTVT_MA_GPLXDACO);
                _model.HV_HS_DVGTVT_MA_GPLXDACO = dvgtvt2.DVGTVT_MA + ":" + dvgtvt2.DVGTVT_TEN;
            }

        
            _model.HV_HS_ACTIVE = (model.HV_HS_ACTIVE == "0" ? false : true);

            initialCategoryEditAction(_model);
            return View(_model);
        }

        [HttpPost]
        public ActionResult Edit(CrudModelDHV_HS model)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<DHV_HS>(o => o.ID==model.ID);
                GPS.COMMON.Helpers.CopyObject<DHV_HS>(model, ref _model, new List<string> { "HV_HS_ACTIVE","HV_HS_NGUOINHAP","HV_HS_NGAYNHAP" });
                if (!string.IsNullOrWhiteSpace(model.HV_HS_DVGTVT_MA))
                {
                    _model.HV_HS_DVGTVT_MA = model.HV_HS_DVGTVT_MA.Split(':')[0];
                }

                if (!string.IsNullOrWhiteSpace(model.HV_HS_DVGTVT_MA_NHANHS))
                {
                    _model.HV_HS_DVGTVT_MA_NHANHS = model.HV_HS_DVGTVT_MA_NHANHS.Split(':')[0];
                }

                if (!string.IsNullOrWhiteSpace(model.HV_HS_DVGTVT_MA_GPLXDACO))
                {
                    _model.HV_HS_DVGTVT_MA_GPLXDACO = model.HV_HS_DVGTVT_MA_GPLXDACO.Split(':')[0];
                }
                _model.HV_HS_NGUOISUA = User.Identity.Name;
                _model.HV_HS_NGAYSUA = DateTime.Now;

               // _model.HV_HS_DATRUYEN = "0";
                if (model.vHV_HS_ANH != null)
                {
                    HttpPostedFileBase file = model.vHV_HS_ANH;
                    using (var binaryReader = new BinaryReader(file.InputStream))
                    {
                        _model.HV_HS_ANH = binaryReader.ReadBytes(file.ContentLength);
                    }

                }

                _db.Update<DHV_HS>(_model);
                return RedirectToAction("Index");
            }

            return View(model);
        }


        #region Delete
        public ActionResult Delete(decimal id)
        {
            DHV_HS model = _db.GetOne<DHV_HS>(o => o.ID == id);
            _db.DeleteItem<DHV_HS>(model);
            return RedirectToAction("Index");
        }
        #endregion

       
        public void initialCategorySearchAction(ViewModelDHV_HS viewModel)
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.pHV_HS_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            //ViewBag.HV_HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");

        }

        public void initialCategoryCreateAction()
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.HV_HS_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.HV_HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");
            ViewBag.HV_HS_HOSO_MAs = new SelectList(_db.GetAll<SHOSO>(), "HOSO_MA", "HOSO_TEN");

            ViewBag.HV_HS_TTRINHXL_MAs = new SelectList(_db.GetAll<STTXL>(), "TTRINHXL_MA", "TTRINHXL_TEN");
            ViewBag.HV_HS_NOIDUNGSHs = new SelectList(_db.GetAll<SNOIDUNGSH>(), "NDSH_MA", "NDSH_TEN");
            ViewBag.HV_HS_MAHTCAPs = new SelectList(_db.GetAll<SHTCAPGPLX>(), "HTGPLX_MA", "HTGPLX_TEN");
        }

        public void initialCategoryEditAction(CrudModelDHV_HS view)
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.HV_HS_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.HV_HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");
            ViewBag.HV_HS_HOSO_MAs = new SelectList(_db.GetAll<SHOSO>(), "HOSO_MA", "HOSO_TEN");

            ViewBag.HV_HS_TTRINHXL_MAs = new SelectList(_db.GetAll<STTXL>(), "TTRINHXL_MA", "TTRINHXL_TEN");
            ViewBag.HV_HS_NOIDUNGSHs = new SelectList(_db.GetAll<SNOIDUNGSH>(), "NDSH_MA", "NDSH_TEN");
            ViewBag.HV_HS_MAHTCAPs = new SelectList(_db.GetAll<SHTCAPGPLX>(), "HTGPLX_MA", "HTGPLX_TEN");
        }

        #region"Ajax"
        public JsonResult AutoCompleteHV_NOITT_DVHC_MA(string term)
        {
            var result = _db.Filter<SDONVIHC>(o => o.DVHC_MA.Contains(term));
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult getHV_MA(string gv_DVGTVT_MA)
        {
            string strHV_MA = gv_DVGTVT_MA + "-" + DateTime.Now.ToString("yyyyMMdd HH:mm:ss").Replace(" ", "-").Replace(":", "");

            return Content(string.Join("", strHV_MA));
        }


        public JsonResult AutoCompleteDVGTVT_MA(string term)
        {
            var result = _db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && (o.DVGTVT_MA.Contains(term) || o.DVGTVT_TEN.ToUpper().Contains(term.ToUpper())));
            return Json(result, JsonRequestBehavior.AllowGet);
        }


        #endregion
	}
}